from common.oss.oss_aliyun import OssAliyun
from common.oss.oss_local import OssLocal
from common.oss.oss_s3 import OssS3
from common.utils.config_reader import get_config

from common.log.logging import log


class OssApiBuilder:

    def __init__(self):
        pass

    def build_client(self):
        oss_type = get_config('oss.type', 'local')
        if 'local' == oss_type:
            base_path = get_config('oss.local.path', './oss_data')
            default_bucket = get_config('oss.local.default-bucket', 'mtl')
            log.info(f"Init local oss client, path:{base_path}, default-bucket:{default_bucket}")
            return OssLocal(base_path, default_bucket)
        elif 's3' == oss_type:
            endpoint = get_config('oss.s3.endpoint', 'http://127.0.0.1:9000')
            access_key = get_config('oss.s3.accessKey', 'minioadmin')
            secret_key = get_config('oss.s3.secretKey', 'minioadmin')
            default_bucket = get_config('oss.s3.default-bucket', 'mtl')
            log.info(f"Init s3 oss client, endpoint:{endpoint}, default-bucket:{default_bucket}")
            return OssS3(endpoint=endpoint,
                                    access_key=access_key,
                                    secret_key=secret_key,
                                    default_bucket=default_bucket)
        elif 'aliyun' == oss_type:
            endpoint = get_config('oss.aliyun.endpoint', 'http://127.0.0.1:9000')
            access_key = get_config('oss.aliyun.accessKey', 'minioadmin')
            secret_key = get_config('oss.aliyun.secretKey', 'minioadmin')
            default_bucket = get_config('oss.aliyun.default-bucket', 'mtl')
            log.info(f"Init aliyun oss client, endpoint:{endpoint}, default-bucket:{default_bucket}")
            return OssAliyun(endpoint=endpoint,
                                        access_key_id=access_key,
                                        access_key_secret=secret_key,
                                        default_bucket=default_bucket)
        elif 'minio' == oss_type:
            endpoint = get_config('oss.minio.endpoint', 'http://127.0.0.1:9000')
            access_key = get_config('oss.minio.accessKey', 'minioadmin')
            secret_key = get_config('oss.minio.secretKey', 'minioadmin')
            default_bucket = get_config('oss.minio.default-bucket', 'mtl')
            log.info(f"Init Minio oss client by S3, endpoint:{endpoint}, default-bucket:{default_bucket}")
            return OssS3(endpoint=endpoint,
                                    access_key=access_key,
                                    secret_key=secret_key,
                                    default_bucket=default_bucket)
